package com.ztesoft.ossup.report.web.config;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.ossup.report.dao.ReportSubRepo;
import com.ztesoft.ossup.report.domain.ReportColumnGroup;
import com.ztesoft.ossup.report.service.ColumnConfService;
import com.ztesoft.ossup.report.web.BaseController;
import com.ztesoft.ossup.report.web.BaseController.ResponseEntry;

@Controller
@RequestMapping("/report/configure/colConf")
public class ColumnConfController extends BaseController{
	Logger log = Logger.getLogger(ColumnConfController.class);

	@Autowired
	ColumnConfService columnConfService;
	
	@RequestMapping(value="/{reportId}" , method = RequestMethod.GET )
	public String colConf(@PathVariable String reportId,HttpServletRequest request){
		request.setAttribute("reportId", reportId);
		String reportCode = columnConfService.getReportCode(Long.parseLong(reportId));
		request.setAttribute("reportCode", reportCode);
//		request.setAttribute("enumStr", columnConfService.getConditionList());
		return "/config/columnConf";
	}
	
//	@RequestMapping(value="/getCols",method = RequestMethod.POST)
//	@ResponseBody
//	public Map queryCols(@RequestBody Map<String, Object> map ){
//		String reportId =  map.get("reportId").toString();
//		Map retMap = columnConfService.getReportCols(Long.valueOf(reportId));
//		return retMap;
//	}
	
	@RequestMapping(value="/getCols",method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> queryCols(@RequestBody Map<String, Object> map ){
		String reportId =  map.get("reportId").toString();
		List<Map<String, Object>> retMap = columnConfService.getReportCols(Long.valueOf(reportId));		
		return retMap;
	}
	
	@RequestMapping(value="/submit/{reportId}",method = RequestMethod.POST)
	@ResponseBody
	public String submitData(@PathVariable Long reportId,@RequestBody List<Map<String, Object>> paramList){
		String retUrl = "";
		//删除表中已有的数据
		columnConfService.deleteByReportId(reportId);
		columnConfService.saveColAndCondition(paramList,reportId);
		String reportCode = columnConfService.getReportCode(reportId);
		if (columnConfService.isHaveCharts(reportId)){
			retUrl =  "/report/chartconfig/toAddChartConfig/"+reportId;
		}else {
			retUrl =  "/report/conditionlist/"+reportCode;
		}
		ResponseEntry responseEntry = new ResponseEntry(SUCCESS,retUrl,null);
		return responseEntry.getJSON();
	}
	
	@RequestMapping(value="/submitGroup",method = RequestMethod.POST)
	@ResponseBody
	public String submitGroupParam(@RequestBody Map<String, Object> params){
		columnConfService.doColumnTable(params); 
		ResponseEntry responseEntry = new ResponseEntry(SUCCESS);
		return responseEntry.getJSON();
	}
	
	@RequestMapping(value="/queryGroup/{reportId}",method = RequestMethod.POST)
	@ResponseBody
	public List<ReportColumnGroup> queryGroupParam(@PathVariable String reportId){
		List<ReportColumnGroup> reportColumnGroupList= columnConfService.queryGroupParam(reportId);
		return reportColumnGroupList;
	}
	
	@RequestMapping(value="/removeGroup/{reportId}",method = RequestMethod.POST)
	@ResponseBody
	public String removeGroupParam(@PathVariable String reportId,@RequestBody List<String> params){
		columnConfService.removeGroupParam(reportId ,params );
		
		ResponseEntry responseEntry = new ResponseEntry(SUCCESS);

		return responseEntry.getJSON();
		
	}
	
}
